//
//  HMHomeTableViewController.swift
//  Weibo-swift
//
//  Created by GD on 16/9/19.
//  Copyright © 2016年 geduo. All rights reserved.
//

import UIKit

private let homeTableViewController : String = "HMHomeTableViewController"

class HMHomeTableViewController: HMVisitorTableViewController {
    
    //懒加载  数组
    lazy var statusList :[HMStatus] =  [HMStatus]()
    
    //使用对应viewModel 对象
    fileprivate lazy var statusListViewModel = HMStatusListViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if !isLogin {
            visitorView?.updateVisitorInfo(message: nil, imageName: nil)
        }else{
            loadData()
            
            setupTableView()

        }

//        setupNavUI()
    }
    
    private func setupTableView(){//注册cell
        tableView.register(HMHomeTableViewCell.self, forCellReuseIdentifier: homeTableViewController)
//        tableView.rowHeight = 200
        
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 200
    }
    
    

    
    //加载首页微博数据
    func loadData()  {
        
        statusListViewModel.loadData { (isSucess) in
            if isSucess {
                self.tableView.reloadData()
            }
        }
        
//        let accessToken = HMUserAccountViewModel.sharedUserAccountViewModel.accessToken!
//        
//        HMNetworkTools.sharedTools.requestStatuses(accessToken: accessToken) { (response, error) in
//            if error != nil{
//                print("网络异常 不是在viewModel中吗 + \(error)")
//                
//                return
//            }
//            
//            print(response)
//            
//            guard let dict = response as? [String: Any] else{
//                print("不是字典")
//                return
//            }
//            
//            guard let statusArray = dict["statuses"] as? [[String: Any]] else{
//                print("不是字典")
//                return
//            }
//            
//            
//            var temparray = [HMStatus]()//数组初始化
//            for statusDict in statusArray{
//                let status = HMStatus(dict: statusDict)
//                
//                temparray.append(status)
//            }
//            
//            //yyModel  字典转模型     如果 有数组嵌套,那么需指定数组中模型的类
//            
//            //记录返回的数据模型, 重新加载数据
//            self.statusList = temparray
//            self.tableView.reloadData()
//            
//        }
    }
    
}
    // MARK: - Table view data source
extension HMHomeTableViewController {

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
//        return statusList.count
        return statusListViewModel.statusList.count

    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: homeTableViewController, for: indexPath) as! HMHomeTableViewCell

//        cell.backgroundColor = UIColor.gray
//        cell.textLabel?.text = statusList[indexPath.row].text
//        cell.textLabel?.text = statusListViewModel.statusList[indexPath.row].user?.screen_name
        cell.statusViewModel = statusListViewModel.statusList[indexPath.row];
        
        return cell
    }
    

    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
